package idiet.service;

import idiet.bean.CurrentId;
import idiet.bean.Meal;
import idiet.bean.MealRecipe;
import idiet.dao.hibernate.CurrentIdDao;
import idiet.dao.hibernate.MealDao;
import idiet.dao.hibernate.MealRecipeDao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("mealService")
public class MealService {
	@Autowired
	MealDao MealDao;
	@Autowired
	MealRecipeDao mealRecipeDao;
	@Autowired
	CurrentIdDao currentIdDao;
	public Meal getMealByMealId(int mealId){
		return this.MealDao.getMealById(mealId);
	}
	public List<Meal> getMealsByMealName(String mealName){
		if(mealName==null||mealName.trim().equals("")){
			return this.MealDao.getMeals();
		}else{
			return this.MealDao.getMealsByMealName(mealName);
		}
	}
	@Transactional
	public void createMeal(Meal meal){
		this.MealDao.createMeal(meal);
		
		List<MealRecipe> recipe=meal.getMealRecipes();
		if(recipe!=null){
			CurrentId maxId=this.currentIdDao.getCurrentIdsByTableName("meal");
			Meal tMeal=new Meal();
			tMeal.setId(maxId.getCurrentMaxId()+1);
			List<MealRecipe> tRecipe=new ArrayList<MealRecipe>();
			for(MealRecipe mr:recipe){
				mr.setMeal(tMeal);
				tRecipe.add(mr);
			}
			this.mealRecipeDao.createMealRecipe(tRecipe);
		}
	}
	public List<Meal> getMeals(String MealName,Date lmTimestampStart,Date lmTimestampEnd){
		/*if(MealName==null){
			return this.MealDao.getMeals(lmTimestampStart, lmTimestampEnd);
		}else{
			return this.MealDao.getMeals(MealName, lmTimestampStart, lmTimestampEnd);
		}*/
		return null;
	}
}
